using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public abstract class UIBaseMapSymbol : UIBaseNode
    {
        public IMapSymbolData data { get; set; }
        public override ResLoader resLoader => UIMapManager.Instance.resLoader;
        public UIMapRoot owner { get; set; }
        public Vector2 size { get; set; }
        public virtual bool updatable => data.config.Updatable;

        protected override void OnDispose()
        {
        }
        
        protected override void OnShow()
        {
#if UNITY_EDITOR
            gameObject.name = $"{data.config.Type.ToString()}_{data.mcid}";
#endif
            Draw();
        }

        protected override void OnHide()
        {
            
        }

        public virtual void Reset()
        {
            data = null;
            owner = null;
            size = Vector2.zero;
        }

        /// <summary>
        /// 提供一个触发Draw的public接口
        /// Draw的逻辑很可能依赖owner,所以不直接暴露出来
        /// Refresh检查一下symbol是否已经有owner,再调用Draw方法
        /// </summary>
        public void Refresh()
        {
            if (owner == null)
            {
                Debug.LogError("No owner for this symbol.Refresh failed");
                return;
            }
            Draw();
        }
        
        protected abstract void Draw();

        protected override void OnUpdate(float dt, float unscaledDt)
        {
            UpdateTransform();
        }
        
        protected void OnInteracted()
        {
            UIMapManager.Instance.SetSelection(this);
            owner.curContainer.HandleSymbolInteract(data);
        }

        protected virtual void UpdateTransform()
        {
            transform.localPosition = owner.PositionWorldToMap(data.worldPosition);
            transform.rotation = data.config.IsShowRotation
                ? owner.RotationWorldToMap(data.worldRotation)
                : Quaternion.identity;
        }

    }
}